package com.demo.controller.buyrecord;

import com.demo.common.model.*;
import com.demo.controller.airroute.AirrouteValidator;
import com.demo.controller.flighttickets.FlightticketsValidator;
import com.demo.interceptor.WebInterceptor;
import com.demo.service.*;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;

import java.math.BigDecimal;

/**
 * 订票记录
 */
@Before(WebInterceptor.class)
public class BuyrecordController extends Controller {

    @Inject
    BuyrecordService buyrecordService;

    @Inject
    FlightticketsService flightticketsService;

    @Inject
    FlightTimeService flightTimeService;

    @Inject
    FlightrecordService flightrecordService;

    public void index() {
        Kv cond = Kv.create();
        //登记姓名
        if (StrKit.notBlank(getPara("username"))) {
            cond.set("username", getPara("username"));
        }
        //电话
        if (StrKit.notBlank(getPara("tel"))) {
            cond.set("tel", getPara("tel"));
        }
        //身份证号
        if (StrKit.notBlank(getPara("identitynum"))) {
            cond.set("identitynum", getPara("identitynum"));
        }
        //航班号
        if (StrKit.notBlank(getPara("flighttimeno"))) {
            cond.set("flighttimeno", getPara("flighttimeno"));
        }
        //状态
        if (StrKit.notBlank(getPara("status"))) {
            cond.set("status", getPara("status"));
        }
        User user = getSessionAttr("user");
        if (user.getRole() == 3) {  //乘客
            cond.set("userid", user.getId());
        }
        setAttr("buyrecordPage", buyrecordService.paginate(getParaToInt(0, 1), 10, Kv.by("cond", cond)));
        render("buyrecord.html");
    }

    public void add() {
        setAttr("flighttickets", flightticketsService.findAll());
    }

    /**
     * 订票
     */
    @Before(BuyrecordValidator.class)
    public void save() {
        User user = getSessionAttr("user");
        int distances=flightrecordService.findDistancesByUserId(user.getId());

        Buyrecord buyrecord = getBean(Buyrecord.class);

        Flighttickets flighttickets=flightticketsService.findById(buyrecord.getFlightticketsid());

        buyrecord.setFlighttimeno(flighttickets.getFlighttimeno());
        buyrecord.setDanjia(flighttickets.getPrice());
        buyrecord.setUserid(user.getId());

        buyrecord.setPrice(buyrecord.getDanjia().multiply(new BigDecimal(buyrecord.getTicketnum())));
        if(distances > 10000){
            buyrecord.setPrice(buyrecord.getDanjia().multiply(new BigDecimal(buyrecord.getTicketnum())).multiply(new BigDecimal(0.8)));
        }
        buyrecord.save();
        redirect("/buyrecord");
    }

    public void edit() {
        setAttr("flighttickets", flightticketsService.findAll());
        setAttr("buyrecord", buyrecordService.findById(getParaToInt()));
    }

    /**
     * 修改订票
     */
    @Before(BuyrecordValidator.class)
    public void update() {
        User user = getSessionAttr("user");
        int distances=flightrecordService.findDistancesByUserId(user.getId());
        Buyrecord buyrecord = getBean(Buyrecord.class);

        Flighttickets flighttickets=flightticketsService.findById(buyrecord.getFlightticketsid());

        buyrecord.setFlighttimeno(flighttickets.getFlighttimeno());
        buyrecord.setDanjia(flighttickets.getPrice());
        buyrecord.setUserid(user.getId());

        buyrecord.setPrice(buyrecord.getDanjia().multiply(new BigDecimal(buyrecord.getTicketnum())));
        if(distances > 10000){
            buyrecord.setPrice(buyrecord.getDanjia().multiply(new BigDecimal(buyrecord.getTicketnum())).multiply(new BigDecimal(0.8)));
        }
        buyrecord.update();
        redirect("/buyrecord");
    }


    public void delete() {
        buyrecordService.deleteById(getParaToInt());
        redirect("/buyrecord");
    }

    /**
     * 结算
     */
    public void settlement(){

        //User user=getSessionAttr("user");

        //订票记录
        Buyrecord buyrecord=buyrecordService.findById(getParaToInt());
        buyrecord.setStatus(1);
        buyrecord.update();

        //航班
        Flighttime flighttime=flightTimeService.findByFlighttimeno(null,buyrecord.getFlighttimeno());

        int distances=flightrecordService.findDistancesByUserId(buyrecord.getUserid());

        //飞行记录
        Flightrecord flightrecord=new Flightrecord();
        flightrecord.setBegin(flighttime.getBegin());
        flightrecord.setEnd(flighttime.getEnd());
        flightrecord.setBegindate(flighttime.getBegindate());
        flightrecord.setEnddate(flighttime.getEnddate());
        flightrecord.setBuyrecordid(getParaToInt());
        flightrecord.setDistance(flighttime.getDistance());
        flightrecord.setUserid(buyrecord.getUserid());
        flightrecord.setDistances(flighttime.getDistance());
        if(distances !=0){
            flightrecord.setDistances(distances+flighttime.getDistance());
        }
        flightrecord.save();

        redirect("/buyrecord");
    }
}
